package com.sinopec.basemodule.dialog;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sinopec.basemodule.R;
import com.sinopec.basemodule.adapter.CommonAdapter;
import com.sinopec.basemodule.adapter.CustomViewHolder;
import com.sinopec.basemodule.model.StockOutErrorCodeDtos;
import com.sinopec.basemodule.model.StockOutSkuErrorModel;
import com.sinopec.basemodule.utils.kotlin.NumberUtils;
import com.sinopec.basemodule.utils.kotlin.StringEKt;
import com.sinopec.basemodule.widget.popupwindow.BasePopup;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @author lwj
 * @ClassName: StockOutSkuErrorPopupWindow
 * @Description: 出库-SKU错误、编码状态错误
 */
public class StockOutSkuErrorPopupWindow extends BasePopup<StockOutSkuErrorPopupWindow> {
    private Context mContext;
    private OnSelectListener listener;
    //异常编码
    private RecyclerView rv_list;
    private StockOutSkuErrorModel model;
    private CommonAdapter<StockOutErrorCodeDtos> adapter;

    //正常编码
    private LinearLayout ll_normal;
    private RecyclerView rv_list_normal;
    private CommonAdapter<String> adapterNormal;
    private ArrayList<String> normalList = new ArrayList<>();

    //关闭
    private View tv_close;
    //确认
    private TextView tv_confirm;
    //业务类型
    private TextView tv_productTypeName;
    //应扫
    private TextView tv_planCount;
    //扫码总数
    private TextView tv_actualCount;
    //正常编码
    private TextView tv_normalCount;
    //SKU错误
    private TextView tv_skuErrorCount;
    //无SKU
    private TextView tv_notSkuCount;
    //编码损坏
    private TextView tv_codeDamageCount;
    //超出计划数
    private TextView tv_overPlanCount;
    //状态异常
    private TextView tv_stateErrorCount;

    public static StockOutSkuErrorPopupWindow create(Context context, StockOutSkuErrorModel model, OnSelectListener listener) {
        return new StockOutSkuErrorPopupWindow(context, model, listener);
    }

    public StockOutSkuErrorPopupWindow(Context context, StockOutSkuErrorModel model, OnSelectListener listener) {
        this.listener = listener;
        this.mContext = context;
        this.model = model;
        setContext(context);
    }

    @Override
    protected void initAttributes() {
        setContentView(R.layout.choose_stock_out_sku_error_popupwindow);
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        //设置弹窗宽高比例IntEKt.dp2px(370)
        this.setWidth((int) (d.widthPixels * 0.90));
        this.setHeight((int) (d.heightPixels * 0.80));

        setFocusAndOutsideEnable(false)//是否PopupWindow 以外触摸dissmiss
                .setBackgroundDimEnable(true)
                .setNeedReMeasureWH(true)
                .setDimValue(0.5f);

//        setContentView(R.layout.choose_warehouse_popupwindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        setFocusAndOutsideEnable(true)
//                .setBackgroundDimEnable(true)
//                .setNeedReMeasureWH(true)
//                .setDimValue(0.5f);
    }

    @Override
    protected void initViews(View view, StockOutSkuErrorPopupWindow basePopup) {
        //业务类型
        tv_productTypeName = view.findViewById(R.id.tv_productTypeName);
        tv_productTypeName.setText(model.getProductTypeName());
        //应扫
        tv_planCount = view.findViewById(R.id.tv_planCount);
        tv_planCount.setText(StringEKt.parseInt(model.getPlanCount()) + "");
        //扫码总数
        tv_actualCount = view.findViewById(R.id.tv_actualCount);
        tv_actualCount.setText(StringEKt.parseInt(model.getActualCount()) + "");
        //正常编码
        tv_normalCount = view.findViewById(R.id.tv_normalCount);
        String num = NumberUtils.add(model.getNormalCount(),model.getAutoFixCodes().size()+"");
        tv_normalCount.setText(num);
        //SKU错误
        tv_skuErrorCount = view.findViewById(R.id.tv_skuErrorCount);
        tv_skuErrorCount.setText(StringEKt.parseInt(model.getSkuErrorCount()) + "");
        //无SKU
        tv_notSkuCount = view.findViewById(R.id.tv_notSkuCount);
        tv_notSkuCount.setText(StringEKt.parseInt(model.getNotSkuCount()) + "");
        //编码损坏
        tv_codeDamageCount = view.findViewById(R.id.tv_codeDamageCount);
        tv_codeDamageCount.setText(StringEKt.parseInt(model.getCodeDamageCount()) + "");
        //超出计划数
        tv_overPlanCount = view.findViewById(R.id.tv_overPlanCount);
        tv_overPlanCount.setText(StringEKt.parseInt(model.getOverPlanCount()) + "");
        //状态异常
        tv_stateErrorCount = view.findViewById(R.id.tv_stateErrorCount);
        tv_stateErrorCount.setText(StringEKt.parseInt(model.getStateErrorCount()) + "");

        //异常编码集合model.getErrorCodes()
        rv_list = view.findViewById(R.id.rv_list);
//        ArrayList<StockOutErrorCodeDtos> list1 = new ArrayList();
//        for (int i = 0; i < 5; i++) {
//            StockOutErrorCodeDtos model = new StockOutErrorCodeDtos();
//            model.setCode("11 12 11 13 13 15");
//            model.setProductTypeName("托盘出库类型"+i);
//            model.setErrorMsg("规格错误" + i);
//            list1.add(model);
//        }
        adapter = new CommonAdapter<StockOutErrorCodeDtos>(mContext, R.layout.stock_out_sku_error_item_popuwindow, model.getErrorCodes()) {
            @Override
            public void convert(CustomViewHolder holder, StockOutErrorCodeDtos sortErrorCodeDtos, int position) {
                View itemView = holder.itemView;
                //异常编码
                TextView tv_code = holder.getView(R.id.tv_code);
                tv_code.setText(sortErrorCodeDtos.getCode());
                //业务类型
                TextView tv_productBusinessName = holder.getView(R.id.tv_productBusinessName);
                tv_productBusinessName.setText(sortErrorCodeDtos.getProductTypeName());
                //异常描述
                TextView tv_errorStateContext = holder.getView(R.id.tv_errorStateContext);
                tv_errorStateContext.setText(sortErrorCodeDtos.getErrorMsg());

                View v_line = holder.getView(R.id.v_line);
                if (Objects.requireNonNull(model.getErrorCodes()).size() == Integer.parseInt((position + 1) + "")) {
                    v_line.setVisibility(View.GONE);
                } else {
                    v_line.setVisibility(View.VISIBLE);
                }
            }
        };
        rv_list.setAdapter(adapter);

        //正常编码集合
        ll_normal = view.findViewById(R.id.ll_normal);
        rv_list_normal = view.findViewById(R.id.rv_list_normal);
//        ArrayList<String> list = new ArrayList();
//        for (int i = 0; i < 5; i++) {
//            list.add("11 12 11 13 13 15");
//        }
        if (model.getNormalCodes() != null) {
            normalList.addAll(model.getNormalCodes());
        }

        if (model.getAutoFixCodes() != null) {
            normalList.addAll(model.getAutoFixCodes());
        }

        if (normalList.size() != 0) {
            ll_normal.setVisibility(View.VISIBLE);
        } else {
            ll_normal.setVisibility(View.GONE);
        }
        adapterNormal = new CommonAdapter<String>(mContext, R.layout.item_rfid_valid, normalList) {
            @Override
            public void convert(CustomViewHolder holder, String code, int position) {
                View itemView = holder.itemView;
                //异常编码
                TextView tv_code = holder.getView(R.id.tv_rfidCode_right);
                tv_code.setText(code);
            }
        };
        rv_list_normal.setAdapter(adapterNormal);

        //确认
        tv_confirm = view.findViewById(R.id.tv_confirm);
        tv_confirm.setOnClickListener((View view1) -> {
            listener.onConfirm();
        });

        //关闭弹窗
        tv_close = view.findViewById(R.id.ll_back);
        tv_close.setOnClickListener((View view1) -> {
            listener.onClose();
        });
    }

    public interface OnSelectListener {
        /**
         * 确认
         */
        void onConfirm();

        /**
         * 关闭
         */
        void onClose();
    }
}
